import os
import sys
import shutil
#Side B,Nedivx
fileDest = "Z:/Media Drive/Video/Movies/"
version = "0.Alpha"

def clear():
    os.system(['clear','cls'][os.name == 'nt'])

def splash():
	print("\n\n")
	print("                                                  ")
	print("      _____         _         _____ _             ")
	print("     |     |___ _ _|_|___ ___|     | |___ ___ ___ ")
	print("     | | | | . | | | | -_|___|   --| | -_| .'|   |")
	print("     |_|_|_|___|\_/|_|___|   |_____|_|___|__,|_|_|")
	print("                                    -by Jo       ")
	print("         Version " + version)
	print("\n\n\n")
	
def titleRoot(title):
	title = title.replace("\\","/")
	title = title.split("/")
	for i in title:
		root = i
	return(root)

## This returns the file extension
def detectExtension(title):
	try:
		return(os.path.splitext(title)[-1].lower())
	except:
		print("\tError with detectExtension")
		
def simpleTitleSearch(title):
	ignoreStrings = ["  "," Avi","-Vip3R"," M4V","Xvid","Mkv","Sparks-","  Torentz 3Xforum Ro","Nedivx","011 R5 Line   Mistere"]
	splitStrings = ["[","("]
	replaceStrings = [["_"," "],[" Side A"," - Part 1"],[" Side B"," - Part 2"],["-Cd1"," - Part 1"],["-Cd2"," - Part 2"],[" Cd1"," - Part 1"],[" Cd2"," - Part 2"],["Sparks-",""],["-",""],["  Part 1"," - Part 1"],["  Part 2"," - Part 2"],["011 R5 Line   Mistere",""],["Torentz 3Xforum Ro",""],["Dvdrip",""],["Avi",""],[" 1983",""]]
	rightStrings = ["-"," "]
	titleClean = ""
	riffTrax = False
	if "Rifftrax" in title:
		riffTrax = True
	if "RiffTrax" in title:
		riffTrax = True
	if "rifftrax" in title:
		riffTrax = True
	title = title.split(".")
	for i in title:
		if i.isdigit():
			break
		else:
			titleClean = titleClean + " " + i
			titleClean = titleClean.lstrip(" ")
			titleClean = titleClean.rstrip(" ")
			titleClean = titleClean.lstrip(" ")
			titleClean = titleClean.rstrip(" ")
			try:
				for s in splitStrings:
					titleClean = titleClean.split(s)
					titleClean = titleClean[0]
			except:
				pass
	titleClean = titleClean.title()
	for i in ignoreStrings:
		titleClean = titleClean.replace(i,"")
	for r in 1,2:
		for i in rightStrings:
			titleClean = titleClean.rstrip(i)
		for i in replaceStrings:
			titleClean = titleClean.replace(i[0],i[1])
	titleClean = titleClean.lstrip(" ")
	titleClean = titleClean.rstrip(" ")
	titleClean = titleClean.lstrip(" ")
	titleClean = titleClean.rstrip(" ")
	if riffTrax == True:
		titleClean = "RiffTrax - " + titleClean
	return(titleClean)

def listDir(d):
	if not os.path.isdir(d):
		files.append(d)
	else:
		for item in os.listdir(d):
			listDir((d + '/' + item) if d != '/' else '/' + item)
			
def buildMoveList():
	pass

def moveFile(moveList):
	totalFiles = len(moveList)
	print("\n====================-Moving " + str(totalFiles) + " File(s)-===================\n")
	totalFiles = 1
	for i in moveList:
		print("Moving " + str(totalFiles) + "/" + str(len(moveList)) + " - " + i[0] + " > " + fileDest + i[1])
		shutil.move(i[0],fileDest + i[1])
		totalFiles += 1

clear()
splash()
print("\t\tBuilding List...")
for i in sys.argv:
	if os.path.isdir(i):
		sourcePath = i
	if os.path.isfile(i):
		sourcePath = i
buildList = []
if os.path.isdir(sourcePath):
	files = []
	listDir(sourcePath)
	for i in files:
		sourceClean = i.replace("\\","/")
		if os.path.getsize(sourceClean) > 100000000:
			buildList.append([sourceClean,simpleTitleSearch(titleRoot(sourceClean)) + detectExtension(sourceClean),True])
else:
	sourceClean = sourcePath.replace("\\","/")
	buildList.append([sourceClean,simpleTitleSearch(titleRoot(sourceClean)) + detectExtension(sourceClean),True])
print("\t\t\t\t...Done\n\n\n\tUsing " + sourcePath + " as source.\n\tUsing " + fileDest + " as destination.\n\n\n\n")
moveList = []
failList = []
for i in buildList:
	if not "sample" in i[1]:
		if not "Sample" in i[1]:
			if not "SAMPLE" in i[1]:
				add = raw_input("Accept [Y/n/New Name]: " + i[1] + " > ")
				if add.upper() == "N":
					failList.append([i[0],i[1]])
				else:
					if add == "":
						moveList.append([i[0],i[1]])
					else:
						moveList.append([i[0],add])

moveFile(moveList)